package net.cnki.sunny.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.github.pagehelper.PageInfo;
import net.cnki.sunny.common.ResultVO;
import net.cnki.sunny.model.dto.RoleDTO;
import net.cnki.sunny.model.entity.Role;
import net.cnki.sunny.model.vo.RoleVO;
import net.cnki.sunny.service.RoleService;
import net.cnki.sunny.validate.BeanValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/role")
    public ResultVO addRole(@RequestBody @Validated(value = BeanValidation.FirstGroup.class) RoleDTO roleDTO) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDTO, role, CopyOptions.create().ignoreNullValue());
        roleService.addRole(role);
        return new ResultVO().success();
    }

    @PutMapping("/role")
    public ResultVO modifyRole(@RequestBody @Validated(value = BeanValidation.SecondGroup.class) RoleDTO roleDTO) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDTO, role, CopyOptions.create().ignoreNullValue());
        roleService.modifyRole(role);
        return new ResultVO().success();
    }

    @GetMapping("/role/user/{id}")
    public ResultVO getNotAssignedRoleUser(@PathVariable("id") Integer id, String username) {
        return new ResultVO().success(roleService.getNotAssignedRoleUser(id, username));
    }

    @PutMapping("/role/users")
    public ResultVO modifyRoleUser(@RequestBody RoleDTO roleDTO) {
        roleService.modifyRoleUser(roleDTO);
        return new ResultVO().success();
    }

    @DeleteMapping("/role/{id}")
    public ResultVO deleteRole(@PathVariable("id") Integer id) {
        roleService.deleteRole(id);
        return new ResultVO().success();
    }

    @GetMapping("/roles")
    public ResultVO getRoleList(RoleDTO roleDTO) {
        return new ResultVO().success(roleService.getRoleList(roleDTO));
    }
}
